/*
 * @FileName: agv_handler.cpp
 * @Author: weibin.yin
 * @Date: 2024-9-11 18:26:52
 * @Last Modified by: 
 * @Last Modified time: 
 */
#include "timer.h"

namespace gallnut {
    
void Slot::append_timer(Timer::ptr timer) {
    if (slot_head == nullptr) {
        slot_head = timer;
        slot_head->next = nullptr;
        slot_head->prev = nullptr;
    } else {
        timer->next = slot_head;
        slot_head->prev = timer;
        slot_head = timer;
    }
}

void Slot::remove_timer(Timer::ptr timer) {
    if (timer == nullptr)
        return;
    if (timer == slot_head) {
        slot_head = slot_head->next;
        if (slot_head != nullptr)
            slot_head->prev = nullptr;
        timer->next = nullptr;
        timer->prev = nullptr;
        return;
    }
    if (timer->next != nullptr)
        timer->next->prev = timer->prev;
    if (timer->prev != nullptr)
        timer->prev->next = timer->next;
}

Timer::ptr Slot::take_one_timer() {
    if (slot_head == nullptr)
        return nullptr;
    Timer::ptr timer = slot_head;
    slot_head = slot_head->next;
    slot_head->prev = nullptr;
    return timer;
}

Timer::ptr Slot::take_all_timer() {
    Timer::ptr timer = slot_head;
    slot_head = nullptr;
    return timer;
}

bool Slot::empty() {
    return slot_head == nullptr;
}

}